use "${gsdAnalysisOutput}/bscie_panel_regressions", clear

keep if wave == 1
replace final_sample = 0 if replacement == 1 & gender == 1

gen attrition = 0
replace attrition = 1 if final_sample == 0


*Define strata
egen strata=group(gender state_old)

quietly tabulate edu_level, gen(edu_level_)
quietly tabulate lit_eval, gen(lit_eval_)
quietly tabulate num_eval, gen(num_eval_)

clear matrix
mat A = J(26,6,.)
mat stars = J(26,6,0)


*Generate IV treatment x kcb_dist 
gen Z_treat_kcb=treatment*lg_kcb_dist
label var Z_treat_kcb "Treatment x (log) distance to KCB branch"

*Generate interaction for other geographic controls
replace num_eval_b = num_eval_b -1
gen treat_citydist=treatment*lg_city_dist
label var treat_citydist "Treatment x (log) distance to city center"
gen treat_roaddist=treatment*lg_road_dist
label var treat_roaddist "Treatment x (log) distance to primary road"
gen treat_gradient=treatment*gradient
label var treat_gradient "Treatment x gradient"
gen treat_conflict = treatment*ACLEDfatalities_x_proxa300
label var treat_conflict "Treatment x conflict_affected_index(300km buffer)"

*Define geographic controls
global geo_controls lg_city_dist treat_citydist treat_roaddist lg_road_dist gradient treat_gradient death_x_prox_a300 treat_conflict 

local n 1

foreach var in age married biz_own  ///
food_spend_total account_yn formal_debt informal_debt ///
edu_level_1  edu_level_2 edu_level_3 edu_level_4 lit_eval_1 lit_eval_2 lit_eval_3  ///
num_eval_1 num_eval_2 num_eval_3  num_eval_4  ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build  attrition  {
	
		reg `var' treatment lg_kcb_dist 1.treatment##c.lg_kcb_dist $geo_controls i.strata, cluster(boma_current)
		
			local beta1 = _b[treatment]
			display `beta1'
			matrix A[`n',1] = `beta1'
			local pvalue1 = (2 * ttail(e(df_r), abs(_b[treatment]/_se[treatment])))
			display `pvalue1'
			matrix A[`n', 2] = `pvalue1'
			local beta2 = _b[lg_kcb_dist]
			display `beta2'
			matrix A[`n',3] = `beta2'
			local pvalue2 = (2 * ttail(e(df_r), abs(_b[lg_kcb_dist]/_se[lg_kcb_dist])))
			display `pvalue2'
			matrix A[`n',4] = `pvalue2'		
			local beta3 = _b[1.treatment#c.lg_kcb_dist]
			display `beta3'
			local pvalue3=(2 * ttail(e(df_r), abs(_b[1.treatment#c.lg_kcb_dist]/_se[1.treatment#c.lg_kcb_dist])))
			display `pvalue3'
			mat A[`n',5] = `beta3'
			mat A[`n',6] = `pvalue3'
	
			matrix stars[`n',1] = (`pvalue1' < 0.10) + (`pvalue1' < 0.05) + (`pvalue1' < 0.01)
			matrix stars[`n',3] = (`pvalue2' < 0.10) + (`pvalue2' < 0.05) + (`pvalue2' < 0.01)
			matrix stars[`n',5] = (`pvalue3' < 0.10) + (`pvalue3' < 0.05) + (`pvalue3' < 0.01)

	local n = `n' + 1

}

mat A[25,5] = `e(N)'

**********************
* Joint orthogonality

gen T_lgkcbdist = treatment*lg_kcb_dist
sum T_lgkcbdist

reg T_lgkcbdist age married  biz_own  ///
food_spend_total  account_yn formal_debt informal_debt ///
edu_level_1  edu_level_2 edu_level_3 edu_level_4 lit_eval_1 lit_eval_2 lit_eval_3  ///
num_eval_1 num_eval_2 num_eval_3  num_eval_4  ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build attrition  $geo_controls i.strata, cluster(boma_current)

test age married  biz_own  ///
food_spend_total account_yn formal_debt informal_debt ///
edu_level_1  edu_level_2 edu_level_3 edu_level_4 lit_eval_1 lit_eval_2 lit_eval_3  ///
num_eval_1 num_eval_2 num_eval_3  num_eval_4  ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build  attrition 

mat A[26,5] = `r(F)'
mat A[26,6] = `r(p)'

********************
* Make the stars for the table
matrix stars[26,5] = (A[26,6] < 0.10) + (A[26,6] < 0.05) + (A[26,6] < 0.01)


*************************
* Make the table

matrix rownames A = "Age" "Married" "Business ownership" "(Log) food consumption food"  ///
"Formal bank account" "Formal loans in past 5 years" "Informal loans in past 5 years"  "Education level: No education" "Education level: Some primary" "Education level: Some secondary" "Education level: Some university" ///
"Literacy: No English" "Literacy: Some English" "Literacy: Good English"   "Numeracy: Low"  "Numeracy: Counting 0-100" "Numeracy: Addition" "Numeracy: Multiplication" ///
 "Household size" "Number of children" "Number of elderly" "Number of rooms" "Number of buildings" ///
  "Attrition" "Observations" "Joint orthogonality" 
frmttable, statmat(A) replace annotate(stars) asymbol(*,**,***) sdec(3)  title(able 3 – Test of instrument correlation with baseline covariates) ctitles("Treatment", "(Log) distance to KCB bank", "Treatment $\times$") plain
frmttable using "$gsdTables/Table3.tex", replay tex replace plain

